// 任务分组控制器
import taskGroupService from '../services/taskGroupService.js';

/**
 * 创建任务分组控制器
 * 处理创建新任务分组的请求
 * @param {Object} request - Fastify请求对象，包含分组名称
 * @param {Object} reply - Fastify响应对象
 * @returns {Object} 创建结果响应
 */
async function createTaskGroup(request, reply) {
  try {
    const { name } = request.body;
    const userId = request.user.userId; // 从认证中间件获取用户ID
    const result = await taskGroupService.createTaskGroup(userId, name);
    return reply.code(201).send({
      code: 1,
      success: true,
      message: '创建成功',
      data: result
    });
  } catch (error) {
    return reply.code(400).send({
      code: 10001,
      success: false,
      message: error.message,
      data: {}
    });
  }
}

/**
 * 获取任务分组列表控制器
 * 处理获取用户任务分组列表的请求
 * @param {Object} request - Fastify请求对象
 * @param {Object} reply - Fastify响应对象
 * @returns {Object} 任务分组列表响应
 */
async function getTaskGroups(request, reply) {
  try {
    const userId = request.user.userId; // 从认证中间件获取用户ID
    const result = await taskGroupService.getTaskGroups(userId);
    return reply.send({
      code: 1,
      success: true,
      message: '获取成功',
      data: result
    });
  } catch (error) {
    return reply.code(500).send({
      code: 10006,
      success: false,
      message: error.message,
      data: {}
    });
  }
}

/**
 * 更新任务分组控制器
 * 处理更新任务分组信息的请求
 * @param {Object} request - Fastify请求对象，包含分组ID和新名称
 * @param {Object} reply - Fastify响应对象
 * @returns {Object} 更新结果响应
 */
async function updateTaskGroup(request, reply) {
  try {
    const { groupId } = request.params;
    const { name } = request.body;
    const userId = request.user.userId; // 从认证中间件获取用户ID
    const result = await taskGroupService.updateTaskGroup(userId, parseInt(groupId), name);
    return reply.send({
      code: 1,
      success: true,
      message: '更新成功',
      data: result
    });
  } catch (error) {
    if (error.message === 'Task group not found or access denied') {
      return reply.code(404).send({
        code: 10004,
        success: false,
        message: error.message,
        data: {}
      });
    }
    return reply.code(400).send({
      code: 10001,
      success: false,
      message: error.message,
      data: {}
    });
  }
}

/**
 * 删除任务分组控制器
 * 处理删除任务分组的请求
 * @param {Object} request - Fastify请求对象，包含分组ID参数
 * @param {Object} reply - Fastify响应对象
 * @returns {Object} 删除结果响应
 */
async function deleteTaskGroup(request, reply) {
  try {
    const { groupId } = request.params;
    const userId = request.user.userId; // 从认证中间件获取用户ID
    await taskGroupService.deleteTaskGroup(userId, parseInt(groupId));
    return reply.send({
      code: 1,
      success: true,
      message: '删除成功',
      data: {}
    });
  } catch (error) {
    if (error.message === 'Task group not found or access denied') {
      return reply.code(404).send({
        code: 10004,
        success: false,
        message: error.message,
        data: {}
      });
    }
    return reply.code(400).send({
      code: 10001,
      success: false,
      message: error.message,
      data: {}
    });
  }
}

export default { createTaskGroup, getTaskGroups, updateTaskGroup, deleteTaskGroup };